//
//  simAddVisitInfoController.m
//  simens
//
//  Created by amy zhang on 12-7-24.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "simAddVisitInfoController.h"
#import "simItemSelectViewController.h"
#import "simShopDetailController.h"

@interface simAddVisitInfoController ()<UITableViewDelegate, UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) deviceItem *currentData;
@property (nonatomic) NSInteger selectedSection;
@property (nonatomic,strong) UITextField *contactorTextField;
@property (nonatomic,strong) UITextField *phoneTextField;
@property (nonatomic,strong) UITextField *opptyidTextField;
@property (nonatomic,strong) NSMutableArray *lastVisitArray;

@end

@implementation simAddVisitInfoController
@synthesize tableView=_tableView;
@synthesize currentData=_currentData;
@synthesize selectedSection=_selectedSection;
@synthesize contactorTextField=_contactorTextField;
@synthesize phoneTextField=_phoneTextField;
@synthesize opptyidTextField=_opptyidTextField;
@synthesize lastVisitArray=_lastVisitArray;


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"添加拜访项目";
	// Do any additional setup after loading the view.
    self.currentData=[[deviceItem alloc] init];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 
                                                                       self.view.bounds.size.height) 
                                                      style:UITableViewStyleGrouped];  
    
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.view addSubview: self.tableView];
    
    UIBarButtonItem* tempRight = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(saveButtonPress)];
    self.navigationItem.rightBarButtonItem = tempRight;
    
    UIBarButtonItem* leftBt=[[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(candelButtonPress)];
    self.navigationItem.leftBarButtonItem=leftBt;
}

-(void) viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];
    [super viewWillAppear:animated]; 
}


-(void) setData:(dictItem *)data{
    if(self.selectedSection==0){
        //NSLog(@"device:%@ ",data.itemname);
        self.currentData.name=data.itemname;
        self.currentData.dev_id=data.itemid;
        for(deviceItem *item in self.lastVisitArray){
            if([item.dev_id isEqualToString:self.currentData.dev_id]){
                self.currentData.contactor=item.contactor;
                self.currentData.phone=item.phone;
                break;
            }
        }
    }else if(self.selectedSection==1){
        //NSLog(@"status:%@",data.itemname);
        self.currentData.sta_id=data.itemid;
        self.currentData.status=data.itemname;
    }
   
}

-(void) setLastVisitDevice:(NSMutableArray *)lastVisitArray{
    self.lastVisitArray=[[NSMutableArray alloc] init];
    self.lastVisitArray=lastVisitArray;
}
-(void) saveButtonPress{
    self.currentData.contactor=self.contactorTextField.text;
    self.currentData.phone=self.phoneTextField.text;
    self.currentData.opptyid=self.opptyidTextField.text;
    NSString *info=@"";
    if(self.currentData.dev_id==nil || [self.currentData.dev_id isEqualToString:@""]){
            info=@"请选择项目";
    }
    else if(self.currentData.sta_id==nil || [self.currentData.sta_id isEqualToString:@""]){
        
        info=@"请选择项目状态";
    }else if(self.currentData.contactor==nil || [self.currentData.contactor isEqualToString:@""]){
        info=@"请填写联系人";
    }else if(self.currentData.phone==nil || [self.currentData.phone isEqualToString:@""]){
        info=@"请填写电话";
    }
//    else if(self.currentData.opptyid==nil || [self.currentData.opptyid isEqualToString:@""]){
//        info=@"请填写opptyid";
//    }
    
    if([info isEqualToString:@""]==NO){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:info
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    }else{
        simShopDetailController *detailController=[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];
        [detailController setdeviceItem:self.currentData];
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] animated:YES];
        
    }
}

-(void) candelButtonPress{
    [self.navigationController popViewControllerAnimated:YES];   
}

-(void) addContactPress{
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}// Default is 1 if not implemented


//tableview行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if(section==2){
        return 3;
    }
    
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString* CellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section, indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    } 
    
    for (UIView *v in [cell subviews]) {
        if ([v isKindOfClass:[UILabel class]] |
            [v isKindOfClass:[UIButton class]]|
            [v isKindOfClass:[UITextField class]]) {
            [v removeFromSuperview];
        }
    }
    
    if (indexPath.section==0) {
        
        cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
        
        UILabel* nameLabel =[[UILabel alloc] initWithFrame:CGRectMake(20, 5, 70, 30)];
        [nameLabel setBackgroundColor:[UIColor clearColor]];        
        [nameLabel setText:@"项目"]; 
        nameLabel.font=[UIFont systemFontOfSize:16];
        [cell addSubview:nameLabel];
        
        UILabel* valueLabel =[[UILabel alloc] initWithFrame:CGRectMake(90, 5, 200, 30)];
        [valueLabel setBackgroundColor:[UIColor clearColor]];        
        [valueLabel setText:self.currentData.name]; 
        valueLabel.font=[UIFont systemFontOfSize:16];
        valueLabel.textAlignment = UITextAlignmentRight;//靠右显示
        [cell addSubview:valueLabel];
        
           
        
        
    }
    
    if(indexPath.section==1){
        cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
        
        UILabel* nameLabel =[[UILabel alloc] initWithFrame:CGRectMake(20, 5, 70, 30)];
        [nameLabel setBackgroundColor:[UIColor clearColor]];        
        [nameLabel setText:@"项目状态"]; 
        nameLabel.font=[UIFont systemFontOfSize:16];
        [cell addSubview:nameLabel];
        
        UILabel* valueLabel =[[UILabel alloc] initWithFrame:CGRectMake(90, 5, 200, 30)];
        [valueLabel setBackgroundColor:[UIColor clearColor]];        
        [valueLabel setText:self.currentData.status]; 
          valueLabel.font=[UIFont systemFontOfSize:16];
          valueLabel.textAlignment = UITextAlignmentRight;
        [cell addSubview:valueLabel];
        
    }
    
    if (indexPath.section==2) {
        //cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        if(indexPath.row==0){
        UILabel* nameLabel =[[UILabel alloc] initWithFrame:CGRectMake(20, 5, 80, 30)];
        [nameLabel setBackgroundColor:[UIColor clearColor]];        
        [nameLabel setText:@"联系人"]; 
        nameLabel.font=[UIFont systemFontOfSize:16];
        [cell addSubview:nameLabel];
        
            self.contactorTextField=[[UITextField alloc] initWithFrame:CGRectMake(100, 10, 150, 30)];
            self.contactorTextField.placeholder=@"点击输入";
            self.contactorTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
            [self.contactorTextField setDelegate:self];
            self.contactorTextField.returnKeyType = UIReturnKeyDone;//设置键盘完成按钮，相应的还有“Return”"Gｏ""Google"等
            self.contactorTextField.text=self.currentData.contactor;
            [cell addSubview:self.contactorTextField];
            
            UIButton *bt=[UIButton buttonWithType:UIButtonTypeContactAdd];
            [bt setFrame:CGRectMake(260, 10, 50, 30)];
            [bt setBackgroundColor:[UIColor clearColor]];
            [bt addTarget:self action:@selector(addContactPress) forControlEvents:UIControlEventTouchUpInside];
           // [cell addSubview:bt];
            
            
            
        }else if(indexPath.row==1){
            UILabel* nameLabel =[[UILabel alloc] initWithFrame:CGRectMake(20, 5, 100, 30)];
            [nameLabel setBackgroundColor:[UIColor clearColor]];        
            [nameLabel setText:@"电话"]; 
            nameLabel.font=[UIFont systemFontOfSize:16];
            [cell addSubview:nameLabel];
            
            self.phoneTextField=[[UITextField alloc] initWithFrame:CGRectMake(100, 10, 180, 30)];
            self.phoneTextField.placeholder=@"点击输入";
            self.phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
            [self.phoneTextField setDelegate:self];
            [self.phoneTextField setKeyboardType:UIKeyboardTypePhonePad];
            self.phoneTextField.returnKeyType = UIReturnKeyDone;//设置键盘完成按钮，相应的还有“Return”"Gｏ""Google"等
            self.phoneTextField.text=self.currentData.phone;
            [cell addSubview:self.phoneTextField];

        }else if(indexPath.row==2){
            UILabel* nameLabel =[[UILabel alloc] initWithFrame:CGRectMake(20, 5, 100, 30)];
            [nameLabel setBackgroundColor:[UIColor clearColor]];        
            [nameLabel setText:@"OPPTYID"]; 
            nameLabel.font=[UIFont systemFontOfSize:16];
            [cell addSubview:nameLabel];
            
            self.opptyidTextField=[[UITextField alloc] initWithFrame:CGRectMake(100, 10, 180, 30)];
            self.opptyidTextField.placeholder=@"点击输入";
            self.opptyidTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
            [self.opptyidTextField setDelegate:self];
            self.opptyidTextField.returnKeyType = UIReturnKeyDone;//设置键盘完成按钮，相应的还有“Return”"Gｏ""Google"等
            self.opptyidTextField.text=self.currentData.opptyid;
            [self.opptyidTextField setHidden:YES];
            [cell addSubview:self.opptyidTextField];

            
            
            
            
            
                                                                                
                                                                                
        }
        
    }
    
    
    return cell;
    
}

//row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
   
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    return @"";
}






- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.selectedSection=indexPath.section;
    if(indexPath.section==0){
        simItemSelectViewController *controller=[[simItemSelectViewController alloc] init];
        [controller setType:@"1" title:@"项目" itemid:self.currentData.dev_id];
        [self.navigationController pushViewController:controller animated:YES];
    }else if(indexPath.section==1){
        simItemSelectViewController *controller=[[simItemSelectViewController alloc] init];
        [controller setType:@"2" title:@"项目状态" itemid:self.currentData.sta_id];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    //NSLog(@"textview did begin editing");
    //键盘遮住了文本字段，视图整体上移
    CGRect frame = self.view.frame;  
    frame.origin.y -=60;  
    frame.size.height +=60;  
    self.view.frame = frame; 
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    //之前视图上移了  现在移回来 
    CGRect frame = self.view.frame;  
    frame.origin.y +=60;  
    frame.size.height -=60;  
    self.view.frame = frame; 
    
    if(textField==self.contactorTextField){
        self.currentData.contactor=textField.text;
    }else if(textField==self.phoneTextField){
        self.currentData.phone=textField.text;
    }else if(textField==self.opptyidTextField){
        self.currentData.opptyid=textField.text; 
    }
}

//按下Done按钮，键盘消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.contactorTextField resignFirstResponder];
    [self.phoneTextField resignFirstResponder];
    [self.opptyidTextField resignFirstResponder];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
